package com.zck.aiagent.util;

import com.zck.aiagent.entity.ChatMessage;
import org.springframework.ai.chat.messages.*;

import java.util.List;
import java.util.Map;

/**
 * ChatMessage <=> Message 转换
 */
public class MessageConverter {
    /**
     * 把ai回答转换成ChatMessage保存到mysql
     * @param message ai回复的消息
     * @param conversationId 当前会话id
     * @return
     */
    public static ChatMessage toChatMessage(Message message,String conversationId){
        return  ChatMessage.builder()
                .conversationId(conversationId)
                .messageType(message.getMessageType())
                .content(message.getText())
                .metadata(message.getMetadata())
                .build();
    }

    public static Message toMessage(ChatMessage chatMessage){
        MessageType messageType = chatMessage.getMessageType();
        String text = chatMessage.getContent();
        Map<String, Object> metadata = chatMessage.getMetadata();
        return switch (messageType){
            case USER -> new UserMessage(text);
            case ASSISTANT ->  new AssistantMessage(text,metadata);
            case SYSTEM ->  new SystemMessage(text);
            case TOOL -> new ToolResponseMessage(List.of(),metadata);
        };
    }
}
